package ui.robots_wicket;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import unq.uis.robots.applicationsModels.ComprarRobotApplicationModel;

public class OfertarPage extends WebPage {

	private static final long serialVersionUID = 1L;
	private ComprarRobotApplicationModel comprarApplicationModel;
	private ComprarPage vistaAnterior;
	
	public OfertarPage(ComprarPage vistaAnterior, ComprarRobotApplicationModel comprarApplicationModel) {
		this.vistaAnterior = vistaAnterior;
		this.comprarApplicationModel = comprarApplicationModel;
		this.agregarLabels();
		this.agregarFormulario();
		this.agregarLinks();
	}

	private void agregarLabels() {
		this.add(new Label("nombreJugador", new PropertyModel<String>(this.comprarApplicationModel, "nombreJugador")));
		this.add(new Label("dineroJugador", new PropertyModel<Integer>(this.comprarApplicationModel, "dineroJugador")));
		this.add(new Label("nombreRobot", new PropertyModel<String>(this.comprarApplicationModel, "nombreRobot")));
		this.add(new Label("danioRobot", new PropertyModel<Integer>(this.comprarApplicationModel, "danioRobot")));
		this.add(new Label("precioBaseRobot", new PropertyModel<Integer>(this.comprarApplicationModel, "precioBaseRobot")));
		
	}

	private void agregarFormulario() {
		Form<?> form = new FormCopado("ofertarForm", this.createModel());
		this.add(form);
		this.addFields(form);
		this.addActions(form);
		form.add(new FeedbackPanel("feedbackPanel"));
		
	}

	private void addActions(final Form<?> form) {
		form.add(new BotonCopado("ofertar") {
			private static final long serialVersionUID = 1L;

			@Override
			public void execute()  {
				OfertarPage.this.comprarApplicationModel.ofertar();
				this.setResponsePage(vistaAnterior.getVistaAnterior());
			}
		});
	}

	private void addFields(Form<?> form) {
		form.add(new TextField<String>("oferta"));	
	}

	protected CompoundPropertyModel<ComprarRobotApplicationModel> createModel() {
		return new CompoundPropertyModel<ComprarRobotApplicationModel>(this.comprarApplicationModel);
	}
	
	private void agregarLinks(){
		this.add(new Link<WebPage>("volver"){
			private static final long serialVersionUID = 1L;

			public void onClick() {
				this.setResponsePage(vistaAnterior);
			};
		}
		);
	}
}
